package com.lx.spring.mvc.web;


import com.lx.spring.mvc.entity.Result;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

@Component
//控制器增强注解
@RestControllerAdvice
public class ExAdvice {

    @ExceptionHandler(MaxUploadSizeExceededException.class )
    public Result maxUploadSizeExceededException(MaxUploadSizeExceededException ex){
        String msg = "上传文件大小不能超过 %s";
        msg = String.format(msg, "1MB");
        return new Result(0, msg, null);
    }

    @ExceptionHandler(EmptyResultDataAccessException.class)
    public Result emptyResultDataAccessException(EmptyResultDataAccessException ex){
        String msg = "用户不存在";
        return new Result(0, msg, null);
    }

    @ExceptionHandler(Exception.class)
    public Result exception(Exception ex){
        ex.printStackTrace();
        return new Result(0, "系统繁忙", null);
    }
}
